import React, { Component } from 'react'

export default class TabControl extends Component {

  constructor(props) {
    super(props)

    this.state = {
      currentIndex: 0,
    }
  }

  render() {
    const {titles} = this.props
    const {currentIndex} = this.state

    return (
      <div className='tab-control'>
        {
          titles.map((item, index) => {
            return (
              <div className={'tab-item ' + (currentIndex === index ? "active" : "")} 
                   key={index}
                   onClick={() => this.itemClick(index)}>
                {item}
              </div>
            )
          })
        }
      </div>
    )
  }

  itemClick(index) {
    this.setState({
      currentIndex: index
    })
    // 拿到父级的itemClick
    const {itemClick} = this.props
    itemClick(index)
  }

}
